package com.atgugui.eduservice.controller.front;

import com.atgugui.commonutils.JwtUtils;
import com.atgugui.commonutils.Result;
import com.atgugui.commonutils.ordervo.CourseWebVoOrder;
import com.atgugui.eduservice.client.OrderClient;
import com.atgugui.eduservice.entity.EduCourse;
import com.atgugui.eduservice.entity.chapter.ChapterVo;
import com.atgugui.eduservice.entity.vo.front.CompletedCourseInfo;
import com.atgugui.eduservice.entity.vo.front.QueryFrontCourseVo;
import com.atgugui.eduservice.service.EduChapterService;
import com.atgugui.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Api(tags = "课程用户页面展示控制层")
@RestController
@RequestMapping("/eduservice/cousrefront")
@CrossOrigin
public class CouresFrontController {

    @Autowired
    private EduCourseService eduCourseService; // 课程service

    @Autowired
    private EduChapterService eduChapterService; // 章节service

    @Autowired
    private OrderClient orderClient; // 注入eduorder模块的接口调用对象

    @ApiOperation(value = "课程展示分页查询")
    @PostMapping("/pageCourse/{current}/{limit}")
    public Result pageCourseByCondition(@PathVariable Long current, @PathVariable Long limit,
                                        @RequestBody(required = false) QueryFrontCourseVo queryFrontCourseVo) {
        Page<EduCourse> coursePage = new Page<>(current,limit);
        HashMap<String, Object> map = eduCourseService.getFrontCoursePageList(coursePage,queryFrontCourseVo);
        return Result.ok().data(map);
    }

    @ApiOperation(value = "根据课程id查询课程详细信息")
    @GetMapping("/getCourseInfoByCouresId/{courseId}")
    public Result getCourseInfoByCourseId(@PathVariable String courseId) {

        return Result.ok();
    }

    /**
     * 有问题
     * 在调用service-order模块中的isBuyCourse(courseId,memberId)接口，抛出status 404 GET 远程接口地址
     * 应该是没有找到这个远程接口，而且偶尔会抛出接口调用超时异常，目前没有解决
     *
     * 解决了：在地址接口地址没有写完整，service-order模块的controller上的映射地址没有写，只写了接口地址
     * 导致无法找到对应的controller层，从而找不到接口
     * @param courseId
     * @param request
     * @return
     */
    @ApiOperation(value = "查询课程的完整的信息")
    @GetMapping("/getCompleteCourseInfo/{courseId}")
    public Result getCompleteCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        // 查询课程完整信息
        CompletedCourseInfo completedCourseInfo = eduCourseService.getCompleteCourseInfo(courseId);
        // 查询该课程对应的章节和视频信息
        List<ChapterVo> chapterVoList = eduChapterService.getChapterVideoByCourseId(courseId);
        // 查询当前用户的课是否支付
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // boolean buyCourse = orderClient.isBuyCourse(courseId,memberId);
        return Result.ok().data("completeCourseInfos",completedCourseInfo)
                          .data("chapterVoList",chapterVoList)
                          .data("buyCourse",true);
    }

    @ApiOperation(value = "自增课程的访问量")
    @GetMapping("/addCourseViewCount/{courseId}")
    public Result addCourseViewCount(@PathVariable String courseId) {
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("course_id",courseId);
        EduCourse eduCourse = eduCourseService.getById(courseId);
        eduCourse.setViewCount(eduCourse.getViewCount() + 1);
        eduCourseService.saveOrUpdate(eduCourse);
        return Result.ok();
    }

    @ApiOperation(value = "根据课程id查询出课程信息(订单模块中使用)")
    @GetMapping("/getCourseInfo/{courseId}")
    public CourseWebVoOrder getCourseInfo(@PathVariable String courseId) {
        CompletedCourseInfo completeCourseInfo = eduCourseService.getCompleteCourseInfo(courseId);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(completeCourseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }

}
